package com.alibaba.test.steed.filter;

import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@ServletComponentScan
@Component
@WebFilter(urlPatterns = {"/server/*"}, filterName = "requestTransFilter")
public class RequestTransFilter implements Filter {
  @Override
  public void init(FilterConfig filterConfig) throws ServletException {

  }

  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    String uri = request.getRequestURI();
    uri = uri.replace("/server", "");
    if (null != uri && "" != uri) {
      request.getRequestDispatcher(uri).forward(request, servletResponse);
    } else {
      filterChain.doFilter(servletRequest, servletResponse);
    }
  }

  @Override
  public void destroy() {

  }
}
